package com.wannengbang.storemobile.mine;


import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.base.BaseActivity;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.UserInfoBean;
import com.wannengbang.storemobile.bean.WechatStateBean;
import com.wannengbang.storemobile.mine.model.MineModelImpl;
import com.wannengbang.storemobile.utils.Base64BitmapUtil;
import com.wannengbang.storemobile.utils.GlideUtils;
import com.wannengbang.storemobile.utils.SPManager;
import com.wannengbang.storemobile.utils.ToastUtil;
import com.wannengbang.storemobile.widget.AppTitleBar;
import com.wannengbang.storemobile.widget.ViewLoading;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WeiXinAuthActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.iv_photo)
    ImageView ivPhoto;
    @BindView(R.id.tv_merchant_no)
    TextView tvMerchantNo;
    @BindView(R.id.tv_tishi)
    TextView tvTishi;
    @BindView(R.id.tv_fz)
    TextView tv_fz;
    @BindView(R.id.iv_dianyin_photo)
    ImageView ivDianyinPhoto;
    private MineModelImpl mineModel = new MineModelImpl();
    private String wechatMerchantNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_xin_auth);
        ButterKnife.bind(this);
        initData();
        tv_fz.setOnClickListener(v -> {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", wechatMerchantNo);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtil.showShort("复制成功");
        });
    }

    public void initData() {

        ViewLoading.showProgress(mActivity);
        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null && userInfoBean.getData() != null) {
            if (userInfoBean.getData().getQuick_income() == 2) {
                ivDianyinPhoto.setVisibility(View.VISIBLE);//显示微信认证图片
                ivPhoto.setVisibility(View.GONE);
                tvMerchantNo.setVisibility(View.GONE);
                mineModel.requestStoreWechatStateDianYin(new DataCallBack<WechatStateBean>() {
                    @Override
                    public void onSuccessful(WechatStateBean bean) {
                        tvTishi.setVisibility(View.VISIBLE);
                        GlideUtils.downloadBitmap(mActivity, ivDianyinPhoto, bean.getData().getQrurl(), new GlideUtils.BitmapLoadListener() {
                            @Override
                            public void getBitmap(Bitmap bitmap) {
                                ivDianyinPhoto.setOnLongClickListener(new View.OnLongClickListener() {
                                    @Override
                                    public boolean onLongClick(View v) {
                                        saveScreenShot(bitmap);
                                        return true;
                                    }
                                });
                            }
                        });

                    }

                    @Override
                    public void onFailed(String msg, String code) {

                    }
                });
            } else {
                ivDianyinPhoto.setVisibility(View.GONE);//隐藏微信认证图片
                ivPhoto.setVisibility(View.VISIBLE);
                tvMerchantNo.setVisibility(View.VISIBLE);
                mineModel.requestStoreWechatState(new DataCallBack<WechatStateBean>() {
                    @Override
                    public void onSuccessful(WechatStateBean bean) {
                        wechatMerchantNo = bean.getData().getWechatMerchantNo();
                        tvTishi.setVisibility(View.VISIBLE);
                        Bitmap bitmap = Base64BitmapUtil.base64ToBitmap(bean.getData().getQrcodeData());
                        ivPhoto.setImageBitmap(bitmap);
                        tvMerchantNo.setText("商户号：" +wechatMerchantNo);
                        ivPhoto.setOnLongClickListener(new View.OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {
                                saveScreenShot(bitmap);
                                return true;
                            }
                        });
                    }

                    @Override
                    public void onFailed(String msg, String code) {

                    }
                });
            }
        }
    }

    /**
     * 屏幕截图保存
     */
    private void saveScreenShot(Bitmap bitmap) {
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        OutputStream outStream = null;
        String filename;//声明文件名
        //以保存时间为文件名
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        filename = sdf.format(date);
        File file = new File(extStorageDirectory, filename + ".JPEG");//创建文件，第一个参数为路径，第二个参数为文件名
        try {
            outStream = new FileOutputStream(file);//创建输入流
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
            outStream.close();

            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            mActivity.sendBroadcast(intent);
            ToastUtil.showShort("保存相册成功");
            Vibrator vibrator = (Vibrator) this.getSystemService(this.VIBRATOR_SERVICE);
            vibrator.vibrate(200);
        } catch (Exception e) {
            ToastUtil.showShort("保存相册失败，可尝试系统截屏保存");
        }
    }
}
